#include "CProfiler.h"
#include <time.h>

CProfiler::CProfiler(string szName)
{
	m_pFile = NULL;
	m_dRunningTotal = 0.0;
	m_llTotalTimes = 0;
	string szPath = "resource/data/";
	string szFileName = szPath + szName + ".txt";
	fopen_s(&m_pFile, szFileName.c_str(), "a+");
	if(!m_pFile)
	{
		MessageBox(0, "Couldn't open file", "!!!!111", MB_OK);
	}
	else
	{
		time_t ltime; /* calendar time */  
		ltime=time(NULL); /* get current cal time */ 
		tm* tm1 = NULL;
		localtime_s(tm1, &ltime);
		char buffer[128];
		fprintf_s(m_pFile, "%s",asctime_s(buffer, _countof(buffer), tm1));
	}
}
CProfiler::~CProfiler()
{
	fprintf_s(m_pFile, "Total Times Run: %d\n", (int)m_llTotalTimes);
	double dAverage = m_dRunningTotal / m_llTotalTimes;
	fprintf_s(m_pFile, "Average Running Time: %f\n\n", dAverage);

	fclose(m_pFile);
}
void CProfiler::Start()
{
	m_CTime.Start();
}
void CProfiler::Stop()
{
	double dTime = m_CTime.GetTime();

	char buffer[400];
	sprintf_s(buffer, _countof(buffer), "%d) Running Time: %f\n", (int)m_llTotalTimes, dTime);

	fprintf_s(m_pFile, buffer);
	m_llTotalTimes++;
	m_dRunningTotal += dTime;

	m_CTime.Reset();
}